from bs4 import BeautifulSoup
import  requests

# 微博热搜话题
url = 'https://s.weibo.com/top/summary?cate=socialevent'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Cookie': 'SUB=_2AkMREM8tf8NxqwFRmfwUyWPib4V3yAvEieKnTD72JRMxHRl-yT9yql4ZtRB6OpDhwllZqXEbhxgL-EJhKOa1Aoq8W-c4; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFA1w_IQVBnUCp1ZuWVZ_4k; _s_tentry=passport.weibo.com; Apache=6846510491108.1045.1716273190557; SINAGLOBAL=6846510491108.1045.1716273190557; ULV=1716273190571:1:1:1:6846510491108.1045.1716273190557:',
'Host': 's.weibo.com',
    'Referer': 'https://s.weibo.com/top/summary',
    'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

resp = requests.get(url, headers=headers, verify=False)
#print(resp.text)

# 解析数据
page = BeautifulSoup(resp.text, 'html.parser')

# 2.找到所有 target="_blank"的对象
result_list = page.findAll(target="_blank")
#print(result_list)

# 3.遍历数组，拿到所有话题
for item in result_list:
    item_name = item.find_next('a').text
    print(item_name)